﻿package KK.net {
	import KK.Queue.IQueueObject;
	import KK.Queue.QObject;
	import KK.System.Pool.IPool;
	import KK.System.Pool.Pool;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	public class Q_Loader extends QObject implements IQueueObject, IPool{
		private var loader:URLLoader;
		private var _url:String;
		private var _params:Object;
		private var _Complete:Function;
		private var _IOError:Function;
		private var _SecurityError:Function;
		private var request:URLRequest;
		
		
		private var pool:IPool;
		public function Q_Loader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=null,_SecurityError:Function=null) {
			// constructor code
			init();
			SetLoader(_url,_Complete,_params,_IOError,_SecurityError);
		}
		private function init():void{
			pool=new Pool;
			status=0;
			loader=new URLLoader();
			request=new URLRequest();
			
			loader.dataFormat=URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE,Loaded);
			loader.addEventListener(IOErrorEvent.IO_ERROR,IO_Error);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,Security_Error);
			 
		}
		//设置Q_Loader对像
		public function SetLoader(_url:String,_Complete:Function ,_params:Object=null,_IOError:Function=null,_SecurityError:Function=null):void{
			status=2;
			reset();
			
			this._url=_url;
			this._Complete=_Complete;
			this._params=_params;
			this._IOError=_IOError;
			this._SecurityError=_SecurityError;
			this.request.data=_params;
			this.request.url=_url; 
			if(_params==null)
			{
				this.request.method=URLRequestMethod.GET;
			}else{
				this.request.method=URLRequestMethod.POST;
			}
			 
		}
		//用object对象赋值
		public function SetLoaderObject(obj:Object):void{
			if(obj.dataFormat!=null||obj.dataFormat!=undefined)
			{
				loader.dataFormat=obj.dataFormat;
			}else{
				loader.dataFormat=URLLoaderDataFormat.BINARY;
			}
			SetLoader(obj.url,obj.Complete,obj.params,obj._IOError,obj._SecurityError);
		}
		//重置对像
		private function reset():void{
			//loader.removeEventListener(Event.COMPLETE,Complete);
			//loader.removeEventListener(IOErrorEvent.IO_ERROR,IO_Error);
			//loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,Security_Error); 
			
			//如果加载重就关闭
			if(status==1)
			{
				loader.close();
			}
			this._url=null;
			this._Complete=null;
			this._params=null;
			this._IOError=null;
			this._SecurityError=null;
			this.request.contentType=null;
		}
		//设置上传数据类型
		public function set contentType(value:String):void{
			this.request.contentType=value;
		}
		
		
		
		
		
		
		
		public function set dataFormat(value:String):void{
			this.loader.dataFormat=value;
		}
		private function Loaded(event:Event):void{
			Complete();
			if(null!=_Complete)
			{
				_Complete(event);
			}
		}
		private function IO_Error(event:IOErrorEvent):void{
			Complete();
			if(null!=_IOError)
			{
				
				_IOError(event);
			}
		}
		private function Security_Error(event:SecurityErrorEvent):void{
			Complete();
			if(null!=_SecurityError)
			{
				_SecurityError(event);
			}
		}
		override public function Start():void
		{
			super.Start();
			load(request);
			//loader.load(request);
		}
		protected function load(request:URLRequest):void{
			loader.load(request);
		}
		
		
		
		
		
		
		
		
		
		/**
		 * 获取状态  
		 * @return 
		 * 
		 */
		public function get PoolStatus():int{
			return pool.PoolStatus;
		}
		
		/**
		 * 未使用标记 
		 * @return 
		 * 
		 */
		public function UnUsed():int{
			return pool.UnUsed();
		}
		
		/**
		 * 使用 
		 * @return 
		 * 
		 */
		public function Using():int{
			return pool.Using();
		}
	}
	
	
	
}
